package com.ts.boot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ts.boot.service.Server;
import com.ts.crawler.pipeline.FlightInfoPipeline;
import com.ts.crawler.scheduler.FlightInfoScheduler;
import com.ts.crawler.selenium.FlightCtripDownLoader;
import com.ts.crawler.selenium.FlightCtripProcesser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
@PropertySource("classpath:config.properties")
public class MainController {

    @Value("${path}")
    private String path;

    @Autowired
    private Server server;

    @RequestMapping("/")
    public String defaultPage(){
        return "myIndex";
    }

    @RequestMapping("/server")
    public void startCrawlerServer(@RequestParam String dCity,@RequestParam String aCity,@RequestParam String sDate,@RequestParam String eDate){
        System.out.println(dCity);
        System.out.println(aCity);
        Spider.create(new FlightCtripProcesser())
                .addPipeline(new FlightInfoPipeline("C:\\Users\\Yuan sir\\Desktop\\lastVersion\\go-path\\Crawler2\\src\\main\\java\\com\\ts\\crawler\\data\\"))//"C:\\Users\\Yuan sir\\Desktop\\新建文件夹\\TSCrawler\\src\\main\\java\\com\\ts\\crawler\\date\\"
                .addPipeline(new ConsolePipeline())
                .setScheduler(new FlightInfoScheduler(server.getUrlList(dCity,aCity,sDate,eDate)))
                .setDownloader(new FlightCtripDownLoader())
                .addUrl("https://flights.ctrip.com/international/search/domestic")
                .thread(1)
                .run();

//http://localhost:8888/server?dCity=%E9%95%BF%E6%B2%99&aCity=%E5%8C%97%E4%BA%AC&sDate=2021-10-23&eDate=2021-10-26
    }

    @RequestMapping("/getResult")
    @ResponseBody
    public JSONObject sendJson() {
        JSONObject result = new JSONObject();
        int count =0;
        result.put("code",0);
        result.put("msg","success");

        String destPath = System.getProperty("user.dir")+"\\data";
//        File jsonFile = new File("C:\\Downloads\\Programming\\Java_Code\\TS_Crawler\\src\\main\\java\\com\\ts\\crawler\\data");
        String newPath ="C:\\Users\\Yuan sir\\Desktop\\lastVersion\\go-path\\Crawler2\\src\\main\\java\\com\\ts\\crawler\\data\\";
        List<String> files = new ArrayList<String>();
        File file = new File(newPath);
        File[] tempList = file.listFiles();

        JSONArray totalResult =new JSONArray();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                String s = readJsonFile(tempList[i].getAbsolutePath());
                JSONObject jsonobj = JSON.parseObject(s);
                JSONArray jsonArray = jsonobj.getJSONArray("result");
                for (int j = 0; j < jsonArray.size(); j++) {
                    totalResult.add(jsonArray.get(j));
                    ++count;
                }
                System.out.println(tempList[i].getName());
                //文件名，不包含路径
                //String fileName = tempList[i].getName();
            }
            if (tempList[i].isDirectory()) {
                //这里就不递归了，
            }
        }
        result.put("count",count);
        result.put("data",totalResult);
        System.out.println(destPath);
        return result;
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String toIndex(){
        // 获取项目根路径
        try {
            File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
            System.out.println(basePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return "myIndex";
    }

    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
